package com.szrt.base;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/**
 * 实体字段处理类
 * @author My
 *
 */
public class FieldUtil {
	
	
	/**
	 *  把一个字段转为实体属性：IS_DELETE  =>isDelete  
	 * @param fieldString	表 字段
	 * @return	实体属性
	 */
	public static String getProperty(String fieldString) {
		
		StringBuffer sb=new StringBuffer();
		
		for(int i=0;i<fieldString.length();i++){
			char  charAt= fieldString.charAt(i);
			if(charAt >= 65&& charAt <= 90){
				sb.append((""+charAt).toLowerCase());
			}else if(charAt=='_'){
				sb.append("");
				i++;
				sb.append(""+fieldString.charAt(i));
			}
		}
//		System.err.println(sb.toString());
		return sb.toString();
	}

	
	/**
	 * 获得数据库表的字段
	 * @param tableName 表名
	 * @return  表字段数组
	 * @throws SQLException
	 */
	public static String[]  getField(String tableName) throws SQLException{
		
		Connection conn = DBUtil.getConn();
		String sql="SELECT * FROM "+tableName;
		PreparedStatement ps = conn.prepareStatement(sql);
		
//		SELECT * FROM 'tmall_product_t'; ''报错
//		ps.setString(1,tableName);
		
		ResultSet rs = ps.executeQuery();
		
		//表结构
		ResultSetMetaData metaData = rs.getMetaData();
		//返回表的字段个数
		int columnCount = metaData.getColumnCount();
		//装表的字段
		String[] fields=new String[columnCount];
		for(int index=0;index<columnCount;index++){
			fields[index] = metaData.getColumnName(index+1);
		}
		
		//ID		TYPE_NAME		FATHER_ID		IS_DELETE
//		for (String st : fields) {
//			System.out.println(st);
//		}
		DBUtil.release(rs, ps);
		return fields;
	}
	
	
	/**
	 * 获得实体字段数组
	 * @return 实体字段数组
	 */
	public static String[] getPropertys(String[] fields){
		
		//ID		TYPE_NAME		FATHER_ID		IS_DELETE  =>isDelete  
//		String[] fields={"ID","TYPE_NAME"};
		int len=fields.length;
		String[] propertys=new String[len];
		
		for(int index=0;index<fields.length;index++){
			String st = fields[index];
			propertys[index] = FieldUtil.getProperty(st);
		}
		
//		for (String ss : propertys) {
//			System.out.println(ss);
//		}
		
		return propertys;
		
	}
	
	/**
	 * 获得表的字段属性
	 * @param tableName 表名
	 * @return
	 * @throws SQLException
	 */
	public static String[] getTypes(String tableName) throws SQLException{
		
		Connection conn = DBUtil.getConn();
		String sql="SELECT * FROM "+tableName;
		PreparedStatement ps = conn.prepareStatement(sql);
//		ps.setString(1,tableName);
		ResultSet rs = ps.executeQuery();
		ResultSetMetaData metaData = rs.getMetaData();
		int columnCount = metaData.getColumnCount();
		String[] types=new String[columnCount];
		for(int index=0;index<columnCount;index++){
			types[index] = metaData.getColumnTypeName(index+1);
		}
		
		//INT VARCHAR	DECIMAL	DECIMAL
		return types;
	}

	/**
	 * 数据库数据类型转换位java数据类型
	 * @param type  数据库类型
	 * @return 实体类型
	 */
	public static String sqlType2javaType(String type){
		//INT VARCHAR	DECIMAL	DECIMAL 
		
		switch (type) {
		case "INT": return "Integer";
		case "VARCHAR": return "String";
		case "DECIMAL": return "Double";
		case "CHAR": return "String";
		case "UNSIGNED ": return "Integer";
		case "BLOB ": return "String";
		case "TINYTEXT ": return "String";
		case "DOUBLE ": return "Double";
		case "DATE ": return "Date";
		case "DATETIME ": return "Datetime";
		

		default:return "String";
		}
		
	} 
	
	/**
	 * 将tableName装换成className   如:tmall_product_type_t ==>Product
	 * @param tableName
	 * @return
	 */
	public static String tableName2ClassName(String tableName){
		
		//  tmall_product_t 或是  tmall_product_type_t  ==>     product
		/*String classname;
		try {
			
			String st1 = tableName.substring(tableName.indexOf("_"),tableName.lastIndexOf("_"));
			classname = st1.substring(st1.indexOf("_")+1,st1.lastIndexOf("_"));
			
		} catch (Exception e) {
			
			String[] split = tableName.split("_");
			classname = split[1];
			
		}
		System.out.println(className);*/
		
		String[] st = tableName.split("_");
		String classname="";
		for(int i=1;i<st.length;i++){
			if("t".equals(st[i])){
				break;
			}else{
				String stn = FieldUtil.toUpperOne(st[i]);
				classname+=stn;
			}
		}
		
		String className="";
		//将首字母变成大写
		for(int index=0;index<classname.length();index++){
			if(index==0){
				char charAt = classname.charAt(index);
				String upperCase = (charAt+"").toUpperCase();
				className+=upperCase;
			}else{
				char charAt = classname.charAt(index);
				className+=charAt;
			}
		}
//		System.out.println(className);
		return className;
	}
	
	/**
	 * 将首个字母变成大写   如：work==》Work
	 * @param work 
	 * @return
	 */
	public static String toUpperOne(String work){
		//将首字母变成大写
		String newWork="";
		for(int index=0;index<work.length();index++){
			if(index==0){
				char charAt = work.charAt(index);
				String upperCase = (charAt+"").toUpperCase();
				newWork+=upperCase;
			}else{
				char charAt = work.charAt(index);
				newWork+=charAt;
			}
		}
		return newWork;
	}
	
	
}
